#include <stdint.h>

#include <encoding_manager.h>
#include <fonts_manager.h>



static int Utf16LE_GetUnicodeFromBuffer(unsigned char *bufstart, unsigned char *bufend, unsigned int *unicode)
{
    if (bufstart + 2 > bufend) 
    {
        return 0;  // 缓冲区不足，无法读取两个字节
    }

    uint16_t first_word = (bufstart[1] << 8) | bufstart[0];
    *unicode = first_word;
    return 2; 
}




// 返回1表示支持Utf16LE编码，返回0表示不支持
static int SupportUtf16LE(unsigned char *bufhead)
{
    const unsigned char utf16le_bom[] = {0xFF, 0xFE};
    
    return (bufhead[0] == utf16le_bom[0] && 
            bufhead[1] == utf16le_bom[1]);
}





static EncodingOpr Utf16LE_EncodingOpr =
{
    .name = "utf16le",
    .iHeadLen = 2, 
    .GetUnicodeFromBuffer = Utf16LE_GetUnicodeFromBuffer,
    .Support = SupportUtf16LE,
};



void RegisterUtf16LE(void)
{
    RegisterEncodingOpr(&Utf16LE_EncodingOpr);
}



// 为Utf16LE_EncodingOpr设置支持Utf16LE编码格式的字体
int SetSupportFontForUtf16LE(void)
{   
    AddFontOprToEncodingOpr(GetFontOprByName("ft"), &Utf16LE_EncodingOpr);
    AddFontOprToEncodingOpr(GetFontOprByName("ascii"), &Utf16LE_EncodingOpr);
    AddFontOprToEncodingOpr(GetFontOprByName("hzk"), &Utf16LE_EncodingOpr);
    return 0;
}

